package com.wang.concurrency;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ProducerConsumer {

	public static void main(String[] args) {
		
		ExecutorService executors = getExecutor();
		Repository repository = new Repository(0,100); 	
		executors.submit(new Producer(repository,30));
		executors.submit(new Producer(repository,30));
		executors.submit(new Consumer(repository,1));
		executors.submit(new Consumer(repository,2));				
		executors.shutdown();
	}
	
	
	
	private static ExecutorService getExecutor(){
		  return new ThreadPoolExecutor(10, 20, 20, TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>());
	}
	
	private static class Producer implements Runnable{
		
		private Repository repository;
		
		private int produceCount;

		public Producer(Repository repository, int produceCount) {
			super();
			this.repository = repository;
			this.produceCount = produceCount;
		}

		@Override
		public void run() {
			while(true){
				repository.add(produceCount);				
			}
			
		}
	}
	
	
	private static class Consumer implements Runnable{
		
		  private Repository repository;

	        private int consumeCount;
	        public Consumer(Repository repository, int consumeCount) {
	            this.repository = repository;
	            this.consumeCount = consumeCount;
	        }

		
		
		@Override
		public void run() {
			while (true) {
				repository.reduce(consumeCount);
            }			
		}
	}
}
